﻿Shader "Custom/ReflectiveUVOffset" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_BumpMap("BumpMap",2d)=""{}
		_ReflectionTex("_ReflectionTex", Cube) = "black" {}
		_DirectionUv("Wet scroll direction (2 samples)", Vector) = (1.0,1.0, -0.2,-0.2)
		_TexAtlasTiling("Tex atlas tiling", Vector) = (8.0,8.0, 4.0,4.0)
		
	}
	
	CGINCLUDE
	#include "UnityCG.cginc"
	struct v2f{
		half4 pos:POSITION;
		half2 uv:TEXCOORD;
		half4 normalScrollUv:TEXCOORD1;
		half4 screen:TEXCOORD2;
		half3 viewDir:TEXCOORD3;
	};
	sampler _MainTex;
	sampler _BumpMap;
	sampler _ReflectionTex;
	float4 _DirectionUv;
	float4 _TexAtlasTiling;
	
	v2f vert(appdata_full i){
		half3 worldPos = mul(_Object2World,i.vertex);
		v2f o;
		o.pos = mul(UNITY_MATRIX_MVP,i.vertex);
		o.uv = i.texcoord;
		o.normalScrollUv = o.uv.xyxy * _TexAtlasTiling + _Time.xxxx * _DirectionUv;
		o.screen = ComputeScreenPos(o.pos);
		o.viewDir = _WorldSpaceCameraPos - worldPos;
		return o;
	}
	half4 frag(v2f i):COLOR{
		half4 bump = tex2D(_BumpMap,i.normalScrollUv.xy);
		bump += tex2D(_BumpMap,i.normalScrollUv.zw);
		
		half4 c = tex2D(_MainTex,i.uv + bump.xy * 0.01);
		
		half3 reflDir = reflect(i.viewDir,bump);
		half4 refl = texCUBE(_ReflectionTex,reflDir);
		
		c += c.a * refl;
		
		return c;
	}
	ENDCG
	
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		Pass{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			ENDCG
		}
	} 
	FallBack "Diffuse"
}
